// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.characters.IsoGameCharacter;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.iso.IsoMovingObject;
import zombie.network.packets.hit.MovingObject;
import zombie.util.Type;
import zombie.vehicles.BaseVehicle;

public class StopSoundPacket implements INetworkPacket {
    MovingObject object = new MovingObject();
    String name;
    boolean trigger;

    public void set(IsoMovingObject obj, String _name, boolean _trigger) {
        this.object.setMovingObject(obj);
        this.name = _name;
        this.trigger = _trigger;
    }

    public void process() {
        IsoMovingObject isoMovingObject = this.object.getMovingObject();
        IsoGameCharacter isoGameCharacter = Type.tryCastTo(isoMovingObject, IsoGameCharacter.class);
        if (isoGameCharacter != null) {
            if (this.trigger) {
                isoGameCharacter.getEmitter().stopOrTriggerSoundByName(this.name);
            } else {
                isoGameCharacter.getEmitter().stopSoundByName(this.name);
            }
        } else {
            BaseVehicle baseVehicle = Type.tryCastTo(isoMovingObject, BaseVehicle.class);
            if (baseVehicle != null) {
                if (this.trigger) {
                    baseVehicle.getEmitter().stopOrTriggerSoundByName(this.name);
                } else {
                    baseVehicle.getEmitter().stopSoundByName(this.name);
                }
            }
        }
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.trigger = b.get() == 1;
        this.object.parse(b, connection);
        this.name = zombie.GameWindow.ReadString(b);
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putByte((byte)(this.trigger ? 1 : 0));
        this.object.write(b);
        b.putUTF(this.name);
    }

    @Override
    public int getPacketSizeBytes() {
        return this.object.getPacketSizeBytes() + 2 + this.name.length();
    }

    @Override
    public String getDescription() {
        return "\n\tStopSoundPacket [name=" + this.name + " | object=" + this.object.getDescription() + "]";
    }
}
